from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db


class BrowseHandler(webapp.RequestHandler):
    """ Create image from bolb """ 
    def get(self, key):
        try:
            if key:
                stockEntity=db.get(db.Key(key))
                self.response.headers['Content-Type'] = 'image/jpeg'
                self.response.out.write(stockEntity.stockIcon)
            else:
                self.redirect('/images/noimage.jpg')
            
        except:
            self.redirect('/images/noimage.jpg')
            

application = webapp.WSGIApplication([(r'/getimage/(.*)', BrowseHandler)
                                     ],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()